package xm.components.radio
{
	import flash.display.DisplayObjectContainer;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	import xm.align.AlignFactory;
	import xm.definition.Arrange;
	import xm.events.SelectionEvent;
	import xm.interfaces.ILifeCycle;
	
	
	
	
	/**
	 * 라디오 버튼 선택 이벤트. 
	 */	
	[Event(name="selectionChange", type="xm.events.SelectionEvent")]
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class RadioGroup extends EventDispatcher implements ILifeCycle, IEventDispatcher
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * 정렬의 시작점 x 
		 */		
		public var x : Number = 0;
		
		/**
		 * 정렬의 시작점 y 
		 */		
		public var y : Number = 0;
		
		/**
		 * 라디오 버튼들 사이의 갭 ( default = 5 );
		 */		
		public var gapBetweenRadioButtons : Number = 5;
		
		/**
		 * arrange 가 "" 일 경우 자동정렬하지 않음.
		 * Arrange = default - Arrange.HORIZONTAL 
		 */		
		public var arrange : String = "horizontal";
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _list : Array = [];
		private var _selectedIndex : int = -1;
		private var _selectedBtn : RadioButton;
		private var _af : AlignFactory = new AlignFactory();
		
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function RadioGroup()
		{
			
		}
		
		

		/**
		 * @param btn
		 */		
		public function addRadio( btn : RadioButton ):void
		{
			_list.push( btn );
			btn.addEventListener( MouseEvent.CLICK, onBtnClick );
		}
		
		
		/**
		 * @param list
		 */		
		public function addRadioList( list : Array ):void
		{
			var i:int, len : int;
			
			i=0;
			len = list.length;
			while( i<len )
			{
				( list[ i ] as RadioButton ).addEventListener(MouseEvent.CLICK, onBtnClick );
				++i;
			}
			_list.concat( list );
		}
		
		/**
		 * @param btn
		 * @param index
		 */		
		public function addRadioAt( btn : RadioButton, index : int ):void
		{
			btn.addEventListener( MouseEvent.CLICK, onBtnClick );
			_list.splice( index, 0 , btn );
		}
		
		/**
		 * @param btn
		 */		
		public function removeRadio( btn : RadioButton ):void
		{
			var idx : int = _list.indexOf( btn );
			btn.removeEventListener( MouseEvent.CLICK, onBtnClick );
			_list.splice( idx, 1 );
		}
		
		/**
		 * @param index
		 */		
		public function removeRadioAt( index : int ):void
		{
			(_list[index] as RadioButton).removeEventListener( MouseEvent.CLICK, onBtnClick );
			_list.splice( index, 1 );
		}
		
		
		/**
		 * <br>arrange == "" 일경우 수동정렬</br>
		 * <br>라디오 버튼들 정렬.</br>
		 * @param parent - 부모.
		 */		
		public function updateDisplayList( parent : DisplayObjectContainer ):void
		{
			if( arrange == "" )
			{
				
			}
			else
			{
				var i:int = 0, len : int = _list.length;
				
				
				_af.destroy();
				_af.setAlignFactory(0,null, arrange);
				_af.setPosition( x, y );
				
				while( i<len )
				{
					if( i > 0 )
					{
						if( arrange == Arrange.HORIZONTAL ){
							_af.addSimpleItem( _list[ i ], [0,0,0,gapBetweenRadioButtons] );
						}else{
							_af.addSimpleItem( _list[ i ], [gapBetweenRadioButtons,0,0,0] );
						}
					}
					else
					{
						_af.addSimpleItem( _list[ i ] );
					}
					++i;
				}
				
				_af.workingAlign();
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		public function createChildren():void
		{
			
		}
		
		/**
		 * createComplete
		 */		
		public function creationComplete():void
		{
			
		}
		
		/**
		 * add
		 */		
		public function add():void
		{
			
		}
		
		/**
		 * remove
		 */		
		public function remove():void
		{
			
		}
		
		
		/**
		 * destroy
		 */		
		public function destroy():void
		{
			var i:int , len : int, radio : RadioButton;
			
			while( i<len )
			{
				radio = _list[ i ] as RadioButton;
				radio.removeEventListener(MouseEvent.CLICK, onBtnClick );
				radio.destroy();
				
				++i;
			}
			_list = null;
			
			x = NaN;
			y = NaN;
			gapBetweenRadioButtons = NaN;
			arrange = null;
			
			_selectedIndex = 0;
			_selectedBtn = null;
			
			_af.destroy();
			_af = null;
			
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		/**
		 * @return 
		 */		
		public function get selectedBtn():RadioButton
		{
			return _selectedBtn;
		}
		
		
		/**
		 * <br>버튼으로 아이템 선택시키기.</br>
		 * @param value
		 */		
		public function set selectedBtn(value:RadioButton):void
		{
			if( selectedBtn ) selectedBtn.selected = false;
			
			if( value ) 
			{
				_selectedBtn = value;
				_selectedBtn.selected = true;
				_selectedIndex = _list.indexOf( _selectedBtn );
			}
		}
		
		
		/**
		 * <br>선택된 아이템의 인덱스 넘버 반환</br>
		 * @return 
		 */		
		public function get selectedIndex():int
		{
			return _selectedIndex
		}
		
		/**
		 * <br>인덱스 번호로 버튼 선택시키기.</br>
		 * -1 = 전체 선택해제
		 * <br />
		 * <br />
		 * @param value
		 */		
		public function set selectedIndex(value:int):void
		{
			_selectedIndex = value;
			
			if( _selectedIndex == -1 )
			{
				selectedBtn = null;
			}
			else
			{
				selectedBtn = _list[ _selectedIndex ] as RadioButton;
			}
				
		}
		
		/**
		 * <br>레이블과 일치하는것으로 버튼 선택시키기.</br>
		 * @param str
		 */		
		public function set selectedLabel( str : String ):void
		{
			var i:int, len:int;
			
			
			i=0;
			len = _list.length;
			
			while( i<len )
			{
				
				if( _list[ i ].label === str ) 
				{
					selectedIndex = i;
					break;
				}
				
				++i;
			}
		}
		
		/**
		 * <br>선택된 버튼의 레이블 반환</br>
		 * @return 
		 */		
		public function get selectedLabel():String
		{
//			return ( selectedIndex > _list.length-1 && _list[ selectedIndex ] ) ? _list[ selectedIndex ].label : "";
			return ( selectedIndex <= _list.length-1 && _list[ selectedIndex ] ) ? _list[ selectedIndex ].label : "";
		}
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * @param e
		 */		
		protected function onBtnClick(e:MouseEvent):void
		{
			var currentBtn : RadioButton = ( e.currentTarget as RadioButton );
			selectedIndex = _list.indexOf( e.currentTarget );
			
			var radioEvent : SelectionEvent = new SelectionEvent( SelectionEvent.SELECTION_CHANGE, false, false );
			radioEvent.data = selectedIndex;
			
			dispatchEvent( radioEvent );
		}
		
		
		
		
	}
}

